package gang.com.mobelhelper.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import gang.com.mobelhelper.AppApplication;
import gang.com.mobelhelper.di.component.AppCompent;
import gang.com.mobelhelper.presenter.BasePresenter;


/**
 * Created by xiaogangzai on 2017/7/18.
 */

public abstract class BaseFragment<T extends BasePresenter> extends Fragment {

    private Unbinder mUnbinder;
    protected AppApplication mApplication;
    private View mRootView;
    @Inject
    T mPresenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(setLayoutId(), container, false);
        mUnbinder = ButterKnife.bind(this, mRootView);
        return mRootView;
    }

    /**
     * 需要注意的是getmAppComponent这些内容需要这个方法调用
     *
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.mApplication = (AppApplication) getActivity().getApplication();
        setupActivityComponent(mApplication.getmAppComponent());
        init();
    }

    public abstract int setLayoutId();

    public abstract void setupActivityComponent(AppCompent appCompent);

    /**
     * 数据初始化方法
     */
    public abstract void init();


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mUnbinder != Unbinder.EMPTY) {
            mUnbinder.unbind();
        }

    }
}
